package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

//封装数据库操作
//DataBaseManager不交给Spring管理,由程序员手动管理
@Slf4j
public class DataBaseManager {
    //从Spring上下文中手动获取MetaMapper的Bean
    private MetaMapper metaMapper;

    //带有业务逻辑的初始化操作
    //完成建库建表操作
    //如果不存在表就进行创建，并添加默认初始数据
    public void init(){
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if(!checkDBExists()){
            //创建data目录
            File file = new File("./data");
            file.mkdirs();
            //建表并初始化数据
            createTable();
            createDefaultData();
            log.info("[DataBaseManager] 数据库初始化完成");
        }else{
            log.info("[DataBaseManager] 数据库已经初始化");
        }
    }

    //删除数据库目录
    //删除目录前,需要保证目录是空的
    public void deleteDB(){
        File file = new File("./data/meta.db");
        //删除目录里面的文件
        boolean isSuccess = file.delete();
        if(isSuccess){
            log.info("[DataBaseManager] 删除数据库文件成功");
        }else{
            log.info("[DataBaseManager] 删除数据库文件失败");
        }
        //删除目录
        file = new File("./data");
        isSuccess = file.delete();
        if(isSuccess){
            log.info("[DataBaseManager] 删除数据库目录成功");
        }else {
            log.info("[DataBaseManager] 删除数据库目录失败");
        }

    }

    //判断数据库(meta.db)是否存在
    private boolean checkDBExists() {
        File DBFile = new File("./data/meta.db");
        return DBFile.exists();
    }

    //对 Exchange,Queue,Binding 进行建表
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        log.info("[DataBaseManager] 创建表完成");
    }

    //RabbitMQ初始带有一个匿名交换机, 类型为 Direct
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        log.info("[DataBaseManager] 初始数据构造完成");
    }

    //增删查操作
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchanges(){
        List<Exchange> exchanges = metaMapper.selectAllExchanges();
        return exchanges;
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(String exchangeName, String queueName){
        metaMapper.deleteBinding(exchangeName, queueName);
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
}
